package com.shundr.user;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.shundr.R;
import com.shundr.base.BaseActivity;
import com.shundr.common.util.DialogUtil;
import com.shundr.common.util.ProgressDialogHelper;
import com.shundr.common.util.ToastHelper;
import com.shundr.common.view.DialogDisplay.OnDisplayListener;
import com.shundr.frame.constant.LContext;
import com.shundr.frame.http.LHttpRequest;
import com.shundr.user.http.UserRequest;
import com.shundr.user.util.CreditsConfig;

/**
 * 
 * CreditsExchangeActivity
 * 
 * @author larry 2015-7-27 上午11:09:32 
 * @version 1.0.0
 *
 */
public class ExchangeDetailActivity extends BaseActivity{
	
	private int mFlag = 0;
	private ImageView ivFare;
	private String mType;
	
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case UserRequest.APPLY_EXCHANGE_SUCCESSED:
				ToastHelper.toastShort(mContext, msg.obj.toString());
				new Timer().schedule(new TimerTask() {
					
					@Override
					public void run() {
						finish();
					}
				}, 600);
				break;
			case LHttpRequest.REQUEST_FAILED:
				ProgressDialogHelper.dismiss();
				ToastHelper.toastShort(mContext, msg.obj.toString());
				break;

			default:
				break;
			}
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_exchange_detail);
		mFlag = getIntent().getIntExtra("flag",0);
		initView();
	}

	private void initView() {
		ivFare = (ImageView)findViewById(R.id.iv_fare);
		final TextView tvCredit = (TextView)findViewById(R.id.tv_credit);
		final TextView tvFare = (TextView)findViewById(R.id.tv_fare);
		switch (mFlag) {
		case 0:
			ivFare.setImageResource(R.drawable.iv_fare_20);
			tvCredit.setText(CreditsConfig.CREDITS_20);
			tvFare.setText("20元(话费直充)");
			mType = "EX_20";
			break;
		case 1:
			ivFare.setImageResource(R.drawable.iv_fare_30);
			tvCredit.setText(CreditsConfig.CREDITS_30);
			tvFare.setText("30元(话费直充)");
			mType = "EX_30";
			break;
		case 2:
			ivFare.setImageResource(R.drawable.iv_fare_50);
			tvCredit.setText(CreditsConfig.CREDITS_50);
			tvFare.setText("50元(话费直充)");
			mType = "EX_50";
			break;
		case 3:
			ivFare.setImageResource(R.drawable.iv_fare_100);
			tvCredit.setText(CreditsConfig.CREDITS_100);
			tvFare.setText("100元(话费直充)");
			mType = "EX_100";
			break;

		default:
			break;
		}
		findViewById(R.id.btn_exchange).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int status = LContext.userInfo.getIdValidationStatus();
				if(status == 0){
					DialogUtil.noticeAlertDialog(mContext, "提示", "您还未提交个人认证,现在去提交吧",
							new OnDisplayListener() {

								@Override
								public void confirm() {
									startActivity(new Intent(mContext, UserAuthActivity.class));
								}

								@Override
								public void cancel() {

								}
							});
				}else if(status == 1){
					ToastHelper.toastLong(mContext, "您的个人认证正在审核中,审核通过后才能兑换话费哦");
					
				}else if(status == 2){
					DialogUtil.noticeAlertDialog(mContext, "兑换", "确认兑换"
							+ tvFare.getText().toString() + "吗?" + "将消耗您"
							+ tvCredit.getText().toString() + "积分哦",
							new OnDisplayListener() {

								@Override
								public void confirm() {
									new UserRequest(mContext, mHandler).applyExchange(mType);
								}

								@Override
								public void cancel() {

								}
							});
				}else if(status == 3){
					DialogUtil.noticeAlertDialog(mContext, "提示", "您的个人认证不符合审核标准,现在去重新提交吧",
							new OnDisplayListener() {

								@Override
								public void confirm() {
									startActivity(new Intent(mContext, UserReAuthActivity.class));
								}

								@Override
								public void cancel() {

								}
							});
				}
						
			}
		});
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
